<?php 
header("content-type:text/html;charset=utf8");
$city = isset($_POST['city'])?$_POST['city']:'北京';
$redis = new Redis();$redis->connect('127.0.0.1','6379');
$pdo = new PDO("mysql:host=127.0.0.1;dbname=liu;charset=utf8",'root','root');

if ($redis->get('city')!=$city) {
	$url = "http://www.liuweather.com/ser.php?city=$city";
	$data = file_get_contents($url);
	$data = json_decode($data,true);
	$res  = $data['result'];
	//print_r($data);
	$i=1;
	foreach ($res as $key => $value) {
		 $redis->hmset($i,$value);$i++;
	}
	$redis->set('i',$i);//存入总条数
	$redis->set('city',$city);
	foreach ($data['result'] as $key => $value) {
		$city= $value['citynm'];
		$date= $value['days'];
		$week = $value['week'];
		$temp_low = $value['temp_low'];
		$temp_high = $value['temp_high'];
		$temperature = $value['temperature'];
		$weather = $value['weather'];
		$pdo->exec("insert into weather values('','$city','$date','$week','$temp_low','$temp_high','$temperature','$weather')");
    }
}  
else {
 	for($i=1; $i <$redis->get('i') ; $i++) 
 	{ 
		$res[]=$redis->hgetall($i);
	}
 } 
    
echo json_encode($res);
    









 ?>